#!/bin/bash -xv
#
# getgroup >>  共通コマンド
#
# Written by yqj 

#環境変数設定
export PATH=/home/SMART_TRIAL:/home/SMART:/usr/local/bin:${PATH}
export LANG=ja_JP.UTF-8

## ログディレクトリの定義
logd=/home/trial/AP/LOG
###走行ログの記録
echo "${logd}/LOG.$(basename $0).$(date +%Y%m%d).$(date +%H%M%S)_$$" &>/dev/null
exec 2>${logd}/LOG.$(basename $0).$(date +%Y%m%d).$(date +%H%M%S)_$$

#変数定義
tmp=/tmp/$$-$(basename $0).$(date +%Y%m%d%H%M%S)
tmpnext=/tmp/$$-tmpnext-$(basename $0).$(date +%Y%m%d%H%M%S)
commonkigyo=0000
kokyakugrpNew=$(cat /MDLINK/${commonkigyo}/TBL/SYS_CONFIG | selrow -e '$1=="kokyakugrpNew"'|selcol -c2)
kokyakugrpCus=$(cat /MDLINK/${commonkigyo}/TBL/SYS_CONFIG | selrow -e '$1=="kokyakugrpCus"'|selcol -c2)
pompad=/home/trial/APDATA/KOKYAGROUP/POMPA

# エラー時のセマフォ処理定義
ERROR_EXIT(){
  exit 1
}

#/////////////////////////////////////////////////////////////
#処理開始
#////////////////////////////////////////////////////////////

PRINT_HELP_AND_EXIT(){
    echo -e "Usage: getgrp [Options] -c <kigyo> -f <filepath> "
    echo -e "  or:  getgroup -f /tmp/aa -c 0001 -u suntory01 "
    echo -e '  /tmp/aa: {"groups": "18", "tab": "s", "groupNames": "test1", "system": "SWITCHING"}" '
    echo -e "Description"
    echo -e "    output:  /tmp/****.hash"
    echo -e "Options:"
    echo -e "    -c, 企業CD"
    echo -e "    -f, paramfile "
    echo -e "    -u, usercd"
    echo -e "    -h, 標準出力に使用方法のメッセージを出力して正常終了する。"
    exit 1
}

#参数处理
while getopts "c:f:u:h" opt; do
    case "$opt" in
    c) kigyocd=${OPTARG} ;;
    f) paramfile=${OPTARG} ;; #是一个文件的路径
    u) usercd=${OPTARG} ;;
    h) PRINT_HELP_AND_EXIT ;;
    esac
done


#检测参数是否完全
if [[ -z "${kigyocd}" || -z "${usercd}" || ! -s ${paramfile} ]];then
    PRINT_HELP_AND_EXIT
fi

groups=$(cat ${paramfile}|jq -r ".groups")
tab=$(cat ${paramfile}|jq -r ".tab")
system=$(cat ${paramfile}|jq -r ".system")

if [ "${tab}" = "k" ];then
    fpath=${pompad}/KOKYAKU_GROUP_CUSTOMIZATION/${kigyocd}
else
    fpath=${pompad}/KOKYAKU_GROUP_NEW/${system}/${usercd}/${kigyocd}
fi
[ -d ${fpath} ] || mkdir -p ${fpath}
[ $(errchk ${PIPESTATUS[@]}) -ne 0 ] && ERROR_EXIT

>${tmp}-groups
>${tmp}-files

echo ${groups} |
sed 's/,/ /g'|
# 1_2 1_3 1_4
tov   |
# 1_2
# 1_3
# 1_4
sed 's/_/\//g' >${tmp}-groupcd

groupNum=$(rowc ${tmp}-groupcd)
first_filename=$(cat ${tmp}-groupcd|head -n1).hash
if [ "${tab}" = "k" ];then
    if [ $(gsutil -q stat ${kokyakugrpCus}/${kigyocd}/${first_filename};echo $?) -eq 0 ];then
        gsutil -m rsync -r -d ${kokyakugrpCus}/${kigyocd} ${fpath}
        [ $(errchk ${PIPESTATUS[@]}) -ne 0 ] && ERROR_EXIT
    fi
else
    if [ $(gsutil -q stat ${kokyakugrpNew}/${system}/${usercd}/${kigyocd}/${first_filename};echo $?) -eq 0 ];then
        gsutil -m rsync -r -d ${kokyakugrpNew}/${system}/${usercd}/${kigyocd} ${fpath}
        [ $(errchk ${PIPESTATUS[@]}) -ne 0 ] && ERROR_EXIT
    fi
fi

if [ ${groupNum} -eq 1 ];then
    tfile=${fpath}"/"${first_filename}
    if [ -s ${tfile} ];then
        cp ${tfile} ${tmpnext}-hashfile.hash
        [ $(errchk ${PIPESTATUS[@]}) -ne 0 ] && ERROR_EXIT
    fi
elif [ ${groupNum} -gt 1 ];then
    while read groupcd;do
        tfile=${fpath}"/"${groupcd}.hash
        if [ -s ${tfile} ];then
            hashtbl_items ${tfile} >>${tmp}-groups
        fi
    done <${tmp}-groupcd
    if [ -s ${tmp}-groups ];then
        ssort -k1,2 ${tmp}-groups |hashtbl -k1 >${tmpnext}-hashfile.hash
        [ $(errchk ${PIPESTATUS[@]}) -ne 0 ] && ERROR_EXIT
    fi
fi

echo ${tmpnext}-hashfile.hash
#/////////////////////////////////////////////////////////////////////////
# 終了処理
#/////////////////////////////////////////////////////////////////////////
#一時ファイルを削除
rm -rf ${tmp}-* &>/dev/null
exit 0
